package com.xmx.ndmallrabbitstarter.config;

import org.springframework.amqp.core.*;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import static com.xmx.ndmallrabbitstarter.constants.RabbitMessageConstants.*;

/**
 * @Description: 用一句话描述
 * @Auther: 新梦想*陈超
 * @Date: 2021/9/4 21:20
 */
@Configuration
@ConditionalOnProperty(name = "spring.order.mq.enabled",matchIfMissing = false,havingValue = "true")
public class OrderMqConfig {


    /*TODO TTL消息 定时*/
    public static class OrderEventConfig{


        /*TODO 1.1个交换机*/
        @Bean
        public TopicExchange orderEventExchange(){
            return new TopicExchange(ORDER_EVENT_EXCHAGE,true,false);
        }
        /*TODO 2.2个队列*/

        /*2.1.普通对队列*/
        @Bean
        public Queue orderCloseQueue(){
            return new Queue(ORDER_CLOSE_QUEUE,true,false,false);
        }
        /*2.1.TTL队列*/
        @Bean
        public Queue orderCreateQueue(){
            return QueueBuilder.durable(ORDER_CREATE_QUEUE)
                    .exclusive()
                    .ttl(120000) //1分钟
                    .deadLetterExchange(ORDER_EVENT_EXCHAGE)
                    .deadLetterRoutingKey(ORDER_CLOSE_DLK)
                    .build();

        }
        /*TODO 3.2个队列绑定到交换机*/
        @Bean
        public Binding orderCreateQueueBinding(){
            return BindingBuilder.bind(orderCreateQueue()).to(orderEventExchange()).with(ORDER_CREATE_BINDING_KEY);
        }

        @Bean
        public Binding orderCloseBinding(){
            return BindingBuilder.bind(orderCloseQueue()).to(orderEventExchange()).with(ORDER_CLOSE_BINDIND_KEY);
        }

    }
}
